-- TD 4.7.4 Try.SNK DRP Connect Try.SNK DRP Test
baselib.report(0x00, "TD4.7.4 beginning.")  -- 测试开始上报

-- 初始化设置
busilib.SetOrientation(1)                   -- 设置USB的CC线,另一根则为Vconn线(这里设置CC1)
busilib.setUsbCcs(CCS.Disabled)             -- 设置CVS为Disabled.SNK
busilib.setUsbRole(ROLE.SNK)
busilib.SetDAP(DAP.VBUS, 8100)              -- 设置VBUS为5V（DAP阻值调整）
baselib.delay(10)                           -- 等待电压稳定

-- 1. CVS transitions to Unattached.SNK
busilib.setUsbCcs(CCS.Unattached)           -- 状态为Unattached.SNK

-- 2. CVS waits 5ms
baselib.delay(5)

-- 3. CVS verifies PUT presents Rp within max dcSRC.DRP x max tDRP [TD.4.7.4.V.1], 验证PUT 在 max dcSRC.DRP × max tDRP 内施加 Rp
baselib.delay(70)                     -- 等待max dcSRC.DRP x max tDRP  =0.7*100
local VCC1 = busilib.GetADC(ADC.CC1, 4)
baselib.report(0x00, "CC1 is " .. VCC1 .. "mv")
if VCC1 > 200 then
    baselib.report(0x00, "TD4.7.4.V.1 pass.")
else
    baselib.report(0x00, "TD4.7.4.V.1 fail.")
end

-- 4. CVS transitions to AttachWait.SNK
busilib.setUsbCcs(CCS.AttachWait)
baselib.delay(5)

-- 5. CVS verifies:
-- 5.a PUT keeps Rp attached since step 3 for tCCDebounce [TD.4.7.4.V.2]
local monitor_rp_duration = busilib.newMonitor(TMD.CHECK_POINT, 0, {2}, {0}, {0})    -- 监控CC1的Rp状态
baselib.delay(200)   --tCCDebounce = 100-200ms
if busilib.getMonitorResult(monitor_rp_duration) == 1 then
    baselib.report(0x00, "TD.4.7.4.V.2 pass.")
else
    baselib.report(0x00, "TD.4.7.4.V.2 fail.")
end
busilib.deleteMonitor(monitor_rp_duration)

-- 5.b PUT removes Rp and presents Rd [TD.4.7.4.V.3]


-- 6. CVS transitions to Unattached.SRC after min tPDDebounce, and on to AttachWait.SRC for max tCCDebounce
baselib.delay(10)   --tPDDebounce = 10-20ms
busilib.setUsbRole(ROLE.Source)
busilib.setUsbCcs(CCS.Unattached)
baselib.delay(200)   --tCCDebounce = 100-200ms
busilib.setUsbCcs(CCS.AttachWait)

-- 7. CVS transitions to Try.SNK for tDRPTry
busilib.setUsbRole(ROLE.Sink)
busilib.setUsbCcs(CCS.Try)
baselib.delay(150)          --tDRPTry=75-150ms

-- 8. CVS verifies PUT presents Rp after tPDDebounce [TD.4.7.4.V.4]
baselib.delay(20)   --tPDDebounce = 10-20ms
VCC1 = busilib.GetADC(ADC.CC1, 4)
baselib.report(0x00, "CC1 is " .. VCC1 .. "mv")
if VCC1 > 200 then
    baselib.report(0x00, "TD4.7.4.V.4 pass.")
else
    baselib.report(0x00, "TD4.7.4.V.4 fail.")
end

-- 9. CVS verifies PUT and CVS transition to Attached.SRC and Attached.SNK  (不理解CVS同时转换成Attached.SRC 和 Attached.SNK)
-- 9.a PUT sources Vbus within tVbusON [TD.4.7.4.V.5]
baselib.delay(275)  -- tVbusON=275ms
if cstmlib.VBusVerified(1) then
    baselib.report(0x00, "TD.4.7.4.V.5 pass.")
else
    baselib.report(0x00, "TD.4.7.4.V.5 fail.")
end

-- 9.b If PUT supports PD: i. PUT and CVS negotiate an explicit contract [TD.4.7.4.V.6]
if USB_PD_Support then
    tcpclib.SetPermission(1, 2)
    local PN_result = tcpclib.PresetPowerNegotiation(1, 500)  -- 500ms timeout
    if PN_result == LuaOk then
        baselib.report(0x00, "TD.4.7.4.V.6 pass.")
    else
        baselib.report(0x00, "TD.4.7.4.V.6 fail.")
    end
end

--9.c If PUT supports USB 3.1: i. PUT starts data communications on its SuperSpeed pairs. [TD.4.7.4.V.7] 

--9.d Else if PUT supports USB 2.0:i. PUT starts data communications on its D+/D- pair. [TD.4.7.4.V.8] 


-- 10. CVS transitions to Disabled
busilib.setUsbCcs(CCS.Disabled)

-- 11. CVS verifies that PUT transitions to Unattached.SNK within tSRCDisconnect,  tSRCDisconnect=0-20ms，通过以下步骤验证PUT在tSRCDisconnect时间内转换成Unattached.SNK
-- 11.a PUT stops sourcing Vbus within tVbusOFF [TD.4.7.4.V.10]
baselib.delay(650)     -- tVbusOFF = 650ms
local VBus = busilib.GetADC(ADC.VBUS, 4)
if VBus < 3670 then
    baselib.report(0x00, "TD.4.7.4.V.10 pass.")
else
    baselib.report(0x00, "TD.4.7.4.V.10 fail.")
end

-- 11.b PUT data communication has ceased. [TD.4.7.4.V.9] 


-- 清理资源
baselib.report(0x00, "TD4.7.4 completed.")
baselib.report(0x07, "\x04")  -- 结束标记